package com.htl.集合;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

public class TreeMap__test2 {
    public static void main(String[] args) {
        TreeMap treeMap = new TreeMap();
        treeMap.put(new C(1), "Java");
        treeMap.put(new C(2), "JavaME");
        treeMap.put(new C(3),"Hello");
        treeMap.put(new C(4), "JavaEE");
        treeMap.put(new C(5), "World");
        treeMap.put(new C(6), "Big");
        treeMap.put(new C(7), "JavaSE");
        Set keySet = treeMap.keySet();
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            System.out.println(key + "---" + treeMap.get(key));
        }
    }
}

class C implements Comparable{
    private int num;

    public C(int num) {
        this.num = num;
    }

    /**
     * A.compareTo(B)
     * 1 A>B
     * 0 A=B
     * -1 A<B
     * @param o
     * @return
     */
    @Override
    public int compareTo(Object o) {
        C c = (C) o;
        if(this.num > c.num) return -1;
        if(this.num == c.num) return 0;
        if(this.num < c.num) return 1;
        return 0;
    }

    @Override
    public String toString() {
        return "C{" +
                "num=" + num +
                '}';
    }
}
